<?php
session_start();
$user =isset($_SESSION['user'])?$_SESSION['user']:false;
// 读取博客列表
require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Db.php';
$db = new Db();

$path='index.php';
$page=isset($_GET['page'])?(int)$_GET['page']:1;
$pagesize=2;


$cid=isset($_GET['cid'])?(int)$_GET['cid']:0;
$where=array();
if($cid){
    $where['cid']=$cid;
    $path .="?cid=$cid";
}
$lists=$db->table('demo_article')->field('id,title,pv,add_time')->where($where)->order('id desc')->pages($page,$pagesize,$path);

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客</title>
    <link rel="stylesheet" type="text/css" href="static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="static/style/index.css">
    <script type="text/javascript" src="static/js/jquery-3.2.0.js"></script>
    <script type="text/javascript" src="static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="static/js/UI.js"></script>
</head>

<body>
    <!-- 顶部栏 -->
    <div class="header">
        <div class="container">
            <span class="title">熙熙熙的Blog</span>
            <div class="search">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="输入博客关键字搜索">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button">搜索</button>
                    </span>
                </div>
            </div>
            <div class="_login-reg">
                <?php if ($user) { ?>
                    <span><?php echo $user['username'] ?></span><a style="color: sandybrown" href="javascript:;" onclick="logout()">退出</a>
                <?php } else { ?>
                    <button type="button" class="btn btn-success" onclick="login()">登录</button>
                <?php } ?>
                <button type="button" class="btn btn-warning" onclick="add_article()">发表博客</button>
            </div>
        </div>
    </div>
    <!-- 中间文章展示区域 -->
    <div class="main container">
        <div class="col-lg-3 lfet-box">
            <p class="cates">博客分类</p>
            <div class="cate-list">
                <div class="cate-item"><a href="index.php?cid=1">PHP</a></div>
                <div class="cate-item"><a href="index.php?cid=2">软件设计</a></div>
                <div class="cate-item"><a href="index.php?cid=3">Web前端</a></div>
                <div class="cate-item"><a href="index.php?cid=4">企业信息化</a></div>
                <div class="cate-item"><a href="index.php?cid=5">安卓开发</a></div>
                <div class="cate-item"><a href="index.php?cid=6">IOS开发</a></div>
                <div class="cate-item"><a href="index.php?cid=7">软件工程</a></div>
                <div class="cate-item"><a href="index.php?cid=8">数据库技术</a></div>
                <div class="cate-item"><a href="index.php?cid=9">操作系统</a></div>
                <div class="cate-item"><a href="index.php?cid=10">其他分类</a></div>
            </div>
        </div>
        <div class="col-lg-9">
            <div class="nav">
                <a href="">热门</a>
                <a href="" class="active">最新</a>
            </div>

            <div class="content-list">

            <?php foreach($lists['data'] as $article){?>
                <div class="content-item">
                    <img src="./static/images/user.png" alt="">
                    <div class="content-tltle">
                        <p><a href="detail.php?aid=<?php echo $article['id'];?>"><?php echo $article['title'];?></a></p>
                        <div><span><?php echo $article['pv'];?></span><span><?php echo date('Y-m-d H:i:s',$article['add_time']);?></span></div>
                    </div>
                </div>
            <?php }?>
            </div>
            <div>
                <?php echo $lists['pages'];?>
            </div>
        </div>
    </div>

</body>

</html>
<script type="text/javascript">
    // 登录
    function login() {
        // UI.alert({title:'系统消息',msg:'请输入用户名',icon:'error'});
        UI.open({
            title: '登录',
            url: './login.php',
            width: 450,
            height: 350
        });
    }

    // 退出登录
    function logout() {
        if (!confirm('确定要退出吗')) {
            return;
        }
        $.get('service/logout.php', {}, function(res) {
            if (res.code > 0) {
                UI.alert({
                    msg: res.msg,
                    icon: 'error'
                });
            } else {
                UI.alert({
                    msg: res.msg,
                    icon: 'ok'
                });
                setTimeout(function() {
                    parent.window.location.reload();
                }, 1000)
            }
        }, 'json')
    }

    // 发表博客
    function add_article(){
        UI.open({title:'发表博客',url:'add_article.php',width:750,height:600});
    }
</script>